package drongo.desktop.utils
{
	import flash.utils.Dictionary;
	
	public class KeyboardCodeUitls
	{
		private static const keyCodes:Dictionary=new Dictionary();
		private static const keyChars:Dictionary=new Dictionary();
		private static var _init:Boolean;
		public function KeyboardCodeUitls()
		{
			
		}
		/**
		 * 通过keyCode来查询按键名称 
		 * @param keyCode
		 * @return 
		 */		
		public static function keyCodeToKeyName(keyCode:uint):String{
			if(!_init){
				init(keyCodesJson);
			}
			var key:String=keyCodes[keyCode];
			return key!=null?key:keyCode+"";
		}
		/**
		 * 通过字母来查找对应的keyCode 
		 * @param keyChar
		 * @return 
		 * 
		 */		
		public static function keyCharToKeyCode(keyChar:String):uint{
			if(!_init){
				init(keyCodesJson);
			}
			return keyChars[keyChar];
		}
		public static function init(data:Object):void{
			var list:Array=data.keyCodes;
			for each (var item:Object in list) 
			{
				keyCodes[item.keyCode]=item.name;
				keyChars[item.name]=item.keyCode;
			}
		}
		private static const keyCodesJson:Object={
			"keyCodes": [
				{
					"name": "BackSpace",
					"keyCode": "8"
				},
				{
					"name": "Tab",
					"keyCode": "9"
				},
				{
					"name": "Clear",
					"keyCode": "12"
				},
				{
					"name": "Enter",
					"keyCode": "13"
				},
				{
					"name": "Shift",
					"keyCode": "16"
				},
				{
					"name": "Ctrl",
					"keyCode": "17"
				},
				{
					"name": "Alt",
					"keyCode": "18"
				},
				{
					"name": "CapsLock",
					"keyCode": "20"
				},
				{
					"name": "Esc",
					"keyCode": "27"
				},
				{
					"name": "Space",
					"keyCode": "32"
				},
				{
					"name": "PageUp",
					"keyCode": "33"
				},
				{
					"name": "PageDown",
					"keyCode": "34"
				},
				{
					"name": "End",
					"keyCode": "35"
				},
				{
					"name": "Home",
					"keyCode": "36"
				},
				{
					"name": "←",
					"keyCode": "37"
				},
				{
					"name": "↑",
					"keyCode": "38"
				},
				{
					"name": "→ ",
					"keyCode": "39"
				},
				{
					"name": "↓",
					"keyCode": "40"
				},
				{
					"name": "Insert",
					"keyCode": "45"
				},
				{
					"name": "Delete",
					"keyCode": "46"
				},
				{
					"name": "Help",
					"keyCode": "47"
				},
				{
					"name": "F1",
					"keyCode": "112"
				},
				{
					"name": "F2",
					"keyCode": "113"
				},
				{
					"name": "F3",
					"keyCode": "114"
				},
				{
					"name": "F4",
					"keyCode": "115"
				},
				{
					"name": "F5",
					"keyCode": "116"
				},
				{
					"name": "F6",
					"keyCode": "117"
				},
				{
					"name": "F7",
					"keyCode": "118"
				},
				{
					"name": "F8",
					"keyCode": "119"
				},
				{
					"name": "F9",
					"keyCode": "120"
				},
				{
					"name": "F10",
					"keyCode": "121"
				},
				{
					"name": "F11",
					"keyCode": "122"
				},
				{
					"name": "F12",
					"keyCode": "123"
				},
				{
					"name": "F13",
					"keyCode": "124"
				},
				{
					"name": "F14",
					"keyCode": "125"
				},
				{
					"name": "F15",
					"keyCode": "126"
				},
				{
					"name": "0",
					"keyCode": "96"
				},
				{
					"name": "1",
					"keyCode": "97"
				},
				{
					"name": "2",
					"keyCode": "98"
				},
				{
					"name": "3",
					"keyCode": "99"
				},
				{
					"name": "4",
					"keyCode": "100"
				},
				{
					"name": "5",
					"keyCode": "101"
				},
				{
					"name": "6",
					"keyCode": "102"
				},
				{
					"name": "7",
					"keyCode": "103"
				},
				{
					"name": "8",
					"keyCode": "104"
				},
				{
					"name": "9",
					"keyCode": "105"
				},
				{
					"name": "*",
					"keyCode": "106"
				},
				{
					"name": "+",
					"keyCode": "107"
				},
				{
					"name": "Enter",
					"keyCode": "108"
				},
				{
					"name": "-",
					"keyCode": "109"
				},
				{
					"name": ".",
					"keyCode": "110"
				},
				{
					"name": "/",
					"keyCode": "111"
				},
				{
					"name": "0",
					"keyCode": "48"
				},
				{
					"name": "1",
					"keyCode": "49"
				},
				{
					"name": "2",
					"keyCode": "50"
				},
				{
					"name": "3",
					"keyCode": "51"
				},
				{
					"name": "4",
					"keyCode": "52"
				},
				{
					"name": "5",
					"keyCode": "53"
				},
				{
					"name": "6",
					"keyCode": "54"
				},
				{
					"name": "7",
					"keyCode": "55"
				},
				{
					"name": "8",
					"keyCode": "56"
				},
				{
					"name": "9",
					"keyCode": "57"
				},
				{
					"name": "A",
					"keyCode": "65"
				},
				{
					"name": "B",
					"keyCode": "66"
				},
				{
					"name": "C",
					"keyCode": "67"
				},
				{
					"name": "D",
					"keyCode": "68"
				},
				{
					"name": "E",
					"keyCode": "69"
				},
				{
					"name": "F",
					"keyCode": "70"
				},
				{
					"name": "G",
					"keyCode": "71"
				},
				{
					"name": "H",
					"keyCode": "72"
				},
				{
					"name": "I",
					"keyCode": "73"
				},
				{
					"name": "J",
					"keyCode": "74"
				},
				{
					"name": "K",
					"keyCode": "75"
				},
				{
					"name": "L",
					"keyCode": "76"
				},
				{
					"name": "M",
					"keyCode": "77"
				},
				{
					"name": "N",
					"keyCode": "78"
				},
				{
					"name": "O",
					"keyCode": "79"
				},
				{
					"name": "P",
					"keyCode": "80"
				},
				{
					"name": "Q",
					"keyCode": "81"
				},
				{
					"name": "R",
					"keyCode": "82"
				},
				{
					"name": "S",
					"keyCode": "83"
				},
				{
					"name": "T",
					"keyCode": "84"
				},
				{
					"name": "U",
					"keyCode": "85"
				},
				{
					"name": "V",
					"keyCode": "86"
				},
				{
					"name": "W",
					"keyCode": "87"
				},
				{
					"name": "X",
					"keyCode": "88"
				},
				{
					"name": "Y",
					"keyCode": "89"
				},
				{
					"name": "Z",
					"keyCode": "90"
				}
				,
				{
					"name": "Command",
					"keyCode": "15"
				},
				{
					"name": "＋",
					"keyCode": "187"
				}
				,
				{
					"name": "－",
					"keyCode": "189"
				}
			]
		}
	}
}